package com.junzhiit.nengji.app.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.app.service.OrdersDataHistoryService;
import com.junzhiit.nengji.app.web.exception.ShopNotFoundException;
import com.junzhiit.nengji.app.web.util.ViewObjectUtil;
import com.junzhiit.nengji.app.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.app.web.vo.OrdersDataHistoryVO;
import com.junzhiit.nengji.app.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.bo.OrdersDataHistoryBO;
import com.junzhiit.nengji.dao.OrdersDataHistoryDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.Shop;

@Service
public class OrdersDataHistoryServiceImpl extends BaseServiceImpl implements OrdersDataHistoryService {

	@Autowired
	private OrdersDataHistoryDAO ordersDataHistoryDAO;
	
	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;
	
	@Autowired
	private ShopDAO shopDAO;
	
	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp, OrdersDataHistoryVO ordersDataHistoryVO) {
		
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(ordersDataHistoryVO.getSessionId()), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		
		OrdersDataHistoryBO ordersDataHistoryBO=new OrdersDataHistoryBO();
		BeanUtils.copyProperties(ordersDataHistoryVO, ordersDataHistoryBO);
		ordersDataHistoryBO.setShopId(shop.getId());
		
		List<OrdersDataHistoryBO> list = ordersDataHistoryDAO.findAll(ordersDataHistoryBO,dtp.getStart(), dtp.getLength());
		int totalCount = ordersDataHistoryDAO.getTotal(ordersDataHistoryBO);
		
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}

}
